package com.best.bestadmin.config;

import com.best.bestadmin.pojo.AdminUser;
import com.best.bestadmin.service.AdminService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;


/**
 * @author yangruyi
 * @date 2021/3/18
 */
public class AdminRealm extends AuthorizingRealm {

    @Autowired
    private AdminService adminService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        AdminUser user = adminService.authentication(token.getUsername());
        if(Objects.isNull(user)){
            throw new UnknownAccountException();
        }

        return new SimpleAuthenticationInfo("",user.getPassword(),"");
    }
}
